<?php

function isRegistered(){
    session_start();

    $user = $_SESSION['session_key'];
    global $db;
    $query = 'SELECT * FROM USERS WHERE `ID`='.$user;
    $result = $db->query($query); 
    $isUser = $result->num_rows;
    if($isUser){
        return true;
    }
    return false;
}

function getUserName(){
    global $user_details;
    
    if($user_details)
        return $user_details['name'].' '.$user_details['lastname'];
    else
        return 'Guest';
}

function getUserID(){
    global $user_details;
    
    if($user_details)
        return $user_details['id'];
    else
        return 0;
}

function getDatesFromRange($start, $end){
    $dates = array($start);
    while(end($dates) < $end){
        $dates[] = date('Y-m-d', strtotime(end($dates).' +1 day'));
    }
    return $dates;
}

function getUserList(){
    global $db;
    
    $query = 'SELECT * FROM USERS';
    $result = $db->query($query);
    
    $users = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($users, array( "userID" => $row['ID'], "userFirstName" => $row['FirstName'], "userLastName" => $row['LastName'], "userEmail" => $row['Email'], "userPhone" => $row['Phone'], "userLevel" => $row['Level']));
        endwhile;
    endif;
    
    return $users;
}

function getHotelList(){
    global $db;
    
    $query = 'SELECT ID, Name, Stars, Address, City, Country, PostalCode, Phone FROM HOTELS';
    $result = $db->query($query); 
    
    $hotels = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($hotels, array( "hotelID" => $row['ID'], "hotelName" => $row['Name'], "hotelStars" => $row['Stars'], "hotelAddress" => $row['Address'], "hotelCity" => $row['City'], "hotelCountry" => $row['Country'], "hotelZIP" => $row['PostalCode'], "hotelPhone" => $row['Phone']));
        endwhile;
    endif;
    
    return $hotels;
}

function getFeaturedHotels(){
    global $db;
    
    $query = 'SELECT ID, Name, Photos, Details FROM HOTELS WHERE `isFeatured` = 1 LIMIT 4';
    $result = $db->query($query); 
    
    $hotels = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($hotels, array( "hotelID" => $row['ID'], "hotelName" => $row['Name'], "hotelPhotos" => $row['Photos'], "hotelDetails" => $row['Details'] ));
        endwhile;
    endif;
    
    return $hotels;
}

function getHotelDetails($id){
    global $db;
    
    $query = 'SELECT * FROM HOTELS WHERE `ID` = ' . $id;
    $result = $db->query($query);
    
    $hotel = array();
    
    if($result):
        $row = $result->fetch_assoc();
        $hotel = array( "hotelID" => $row['ID'], "hotelName" => $row['Name'], "hotelStars" => $row['Stars'], "hotelAddress" => $row['Address'], "hotelCity" => $row['City'], "hotelCountry" => $row['Country'], "hotelZIP" => $row['PostalCode'], "hotelPhone" => $row['Phone'], "hotelDetails" => $row['Details'], "hotelEmail" => $row['Email'], "hotelWebsite" => $row['Website'], "hotelPhotos" => $row['Photos'], "hotelFacilities" => $row['Facilities'], "hotelCoords" => $row['Coordinates'], "isFeatured" => $row['isFeatured']);
    endif;
    
    return $hotel;
}

function getRoomByID($id){
    global $db;
    $query = 'SELECT  Details, Type, Price, Photo, Guests FROM ROOM_TYPES WHERE `ID` = '.$id .' ORDER BY `Price` DESC';
    $result = $db->query($query); 
    
    $roomDetails = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            $roomDetails = array( "roomType" => $row['Type'], "roomDetails" => $row['Details'], "roomPrice" => $row['Price'], "roomPhotos" => $row['Photo'], "roomGuests" => $row['Guests']);
        endwhile;
    endif;
    
    return $roomDetails;
}

function getReservations(){
    global $db;
    
    $query = 'SELECT RESERVATIONS.ID as resID, RESERVATIONS.DateIn as dateIn, RESERVATIONS.DateOut as dateOut, RESERVATIONS.PriceSummary as price, USERS.FirstName as firstname, USERS.LastName as lastname, ROOM_TYPES.Type as room, HOTELS.Name as hotel 
             FROM RESERVATIONS
             INNER JOIN USERS ON USERS.ID = RESERVATIONS.IDUser
             INNER JOIN ROOM_TYPES ON ROOM_TYPES.ID = RESERVATIONS.IDRoom
             INNER JOIN HOTELS ON HOTELS.ID = ROOM_TYPES.Hotel';
    $result = $db->query($query);
    
    $reservations = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($reservations, array( "resID" => $row['resID'], "dateIn" => $row['dateIn'], "dateOut" => $row['dateOut'], "price" => $row['price'], "customer" => $row['firstname'].' '.$row['lastname'], "room" => $row['room'], "hotel" => $row['hotel']));
        endwhile;
    endif;
    
    return $reservations;
}

//epistrefei kapoia stoixeia olov tvn jenodoxivn, xrisimopieite apo listings.php 
function getHotelListF(){
    global $db;
    
    $query = 'SELECT ID, Name, Stars, City, Country, Photos FROM HOTELS ORDER BY `Stars` DESC';
    $result = $db->query($query); 
    
    $hotels = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($hotels, array( "hotelID" => $row['ID'], "hotelName" => $row['Name'], "hotelStars" => $row['Stars'], "hotelCity" => $row['City'], "hotelCountry" => $row['Country'], "hotelPhotos" => $row['Photos']));
        endwhile;
    endif;
    
    return $hotels;
}

function getRoomDetails($id){
    global $db;
    
    $query = 'SELECT * FROM ROOM_TYPES WHERE `Hotel` = '.$id .' ORDER BY `Guests` ASC';
    $result = $db->query($query); 
    
    $hotelRooms = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($hotelRooms, array( "roomID" => $row['ID'], "roomType" => $row['Type'], "roomDetails" => $row['Details'], "roomPrice" => $row['Price'], "roomPhoto" => $row['Photo'], "roomGuests" => $row['Guests'], "roomNumber" => $row['Rooms']));
        endwhile;
    endif;
    
    return $hotelRooms;
}

//epistrefei thn mikroteri timh apo ta dvmatia enos jenodoxeioy, xrisimopieite apo listings.php kai hotel.php
function getsmallerprice($id){
    global $db;
 
    $query = 'SELECT Price FROM ROOM_TYPES WHERE `HOTEL` = ' . $id;
    $result = $db->query($query);
  
    if($result):
    	$row = $result->fetch_assoc();
    	$minprice = $row['Price'];
    	while ($row = $result->fetch_assoc()):
        	if ($row['Price'] < $minprice):
        		$minprice = $row['Price'];
        	endif;	
       endwhile;
    endif;
    
    return $minprice;
}
//epistrefei kapoia stoixeia tvn dvmativn enos jenodexeiou, xrisimopoieite sto hotels.php 
function getRoomTypesList($id){
    global $db;
    
    $query = 'SELECT  ID, Details, Type, Price, Photo, Guests FROM ROOM_TYPES WHERE `Hotel` = '.$id .' ORDER BY `Price` DESC';
    $result = $db->query($query); 
    
    $roomtypes = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($roomtypes, array( "roomType" => $row['Type'], "roomDetails" => $row['Details'], "roomPrice" => $row['Price'], "roomPhotos" => $row['Photos'], "roomGuests" => $row['Guests'], "roomID" => $row['ID']));
        endwhile;
    endif;
    
    return $roomtypes;
}

function getRoomTypeIDs($id){
    global $db;
    
    $query = 'SELECT ID FROM ROOM_TYPES WHERE `Hotel` = '.$id .' ORDER BY `Price` DESC';
    $result = $db->query($query); 
    
    $roomtypeIDs = array();
    
    if($result):
        while ($row = $result->fetch_assoc()):
            array_push($roomtypeIDs, array( "id" => $row['ID']));
        endwhile;
    endif;
    
    return $roomtypeIDs;
}

function returnHotelName($id){	// kostas
	global $db;
	
	$query = 'SELECT Name FROM HOTELS WHERE ID ='.id ;
	$result = $db->query($query); 
	return $result;
}

function returnHotelName2($id){
	global $db;
	
	$query = 'SELECT Name FROM HOTELS WHERE ID ='.id ;
	$result = $db->query($query); 
	if($result):
        while ($row = $result->fetch_assoc()):
            return $row['Name'];
        endwhile;
    endif;
}

function returnRoomType($id){
	global $db;
	
	$query = 'SELECT Type FROM ROOM_TYPES WHERE ID ='.id ;
	$result = $db->query($query); 
	if($result):
        while ($row = $result->fetch_assoc()):
            return $row['Type'];
        endwhile;
    endif;
}

function getUserInfo(){	// gravanis
    
    global $user_details;
    
    $info = array();

    if($user_details){
        $info = array( "name" => $user_details['name'], "lastname" => $user_details['lastname'], 
		"email" => $user_details['email'], "phone" => $user_details['phone'], "id" 		
		=> $user_details['ID'],"password" => $user_details['password']);
        return $info;
    }
    else 
        return 'Guest';
}

function Login(){
    global $user_details1;
    
    if($user_details1)
        return $user_details1['name'].' '.$user_details['lastname'];
    else
        return 'Login';
}

function NameShit(){
    
    session_start();
    
    if(!isset($_SESSION['session_key']))
    {return "Login";}
    else{
       $user = $_SESSION['session_key'];
        global $db;
        global $user_details;

        $query = 'SELECT * FROM USERS WHERE `ID`='.$user;
        $result = $db->query($query); 
        $isUser = $result->num_rows;
        if($isUser){
        $user = $result->fetch_assoc();
        $user_details['name'] = $user['FirstName'];
        $user_details['lastname'] = $user['LastName'];
    
return $user_details['name'].' '.$user_details['lastname'];
}
}
}

function Logout(){
    
    session_start();
    
    if(!isset($_SESSION['session_key']))
    {return "";}
    else{
        return "Logout";}
}




    
?>